home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / pixels.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-13  |  2.4 KB  |  100 lines  |  [TEXT/CWIE]

  1. #include "gltron.h"
  2.  
  3. #ifndef TEST
  4. unsigned char* loadPixels(char *filename, gDisplay *d) {
  5.   texture* tex;
  6.   /* load a texture, scale it to the appropriate size */
  7.   tex = loadTextureData(filename);
  8.   return scalePixels(tex->data, 
  9.              tex->width, tex->height,
  10.              0, 0,
  11.              tex->width, tex->height,
  12.              d->vp_w, d->vp_h, 
  13.              tex->channels);
  14. }
  15. #endif
  16.  
  17. unsigned char *scalePixelBitmap(unsigned char *source, int sw, int sh, 
  18.                int posx, int posy, int width, int height,
  19.                int dw, int dh, int bytes) {
  20.   int x, y;
  21.   unsigned char *data;
  22.   unsigned char byte;
  23.   int i;
  24.  
  25.   int row;
  26.  
  27.   row = dw / 8;
  28.   if(dw % 8) row++;
  29.   
  30.   data = (unsigned char*) malloc( row * dh );
  31.   for(y = 0; y < dh; y++) {
  32.     byte = 0;
  33.     for(x = 0; x < dw; x++) {
  34.       int sx, sy;
  35.       sx = posx + ((x * width) / dw);
  36.       sy = posy + ((y * height) / dh);
  37.       for(i = 0; (i < bytes) && (i < 3); i++) {
  38.     if(source[bytes * (sx + sy * sw) + i] != 0) {
  39.       /* printf("source value: (%d %d, %d): %d\n", 
  40.          sx, sy, i, source[bytes * (sx + sy * sw) + i]); */
  41.       byte |= (1 << (x % 8) );
  42.       break;
  43.     } else {
  44.       /* printf("source value: (%d %d, %d): %d\n", 
  45.          sx, sy, i, source[bytes * (sx + sy * sw) + i]); */
  46.     }
  47.       }
  48.       if((x % 8) == 7) {
  49.     data[x / 8 + y * row] = byte;
  50.       }
  51.     }
  52.     if(x % 8)
  53.       data[x / 8 + y * row] = byte;
  54.     byte = 0;
  55.   }
  56.   return data;
  57. }
  58.  
  59. unsigned char *scalePixels(unsigned char *source, int sw, int sh, 
  60.                int posx, int posy, int width, int height,
  61.                int dw, int dh, int bytes) {
  62.   int x, y;
  63.   unsigned char *data;
  64.   /*
  65.   fprintf(stderr, "scaling from (%d,%d-%d,%d) in a (%d, %d)"
  66.       "to (%d,%d), bytes: %d\n", posx, posy, posx + width, posy + width,
  67.       sw, sh, dw, dh, bytes);
  68.   */
  69.   data = (unsigned char*) malloc( dw * dh * bytes );
  70.   for(y = 0; y < dh; y++) {
  71.     for(x = 0; x < dw; x++) {
  72.       int sx, sy, j;
  73.       int sx2, sy2;
  74.       
  75.       sx = posx + ((x * width) / dw);
  76.       sy = posy + ((y * height) / dh);
  77.       (sx == sw - 1) ? (sx2 = sx - 1) : (sx2 = sx + 1);
  78.       (sy == sh - 1) ? (sy2 = sy - 1) : (sy2 = sy + 1);
  79.       /* printf("%d, %d to %d, %d\n", x, y, sx, sy); */
  80.       for(j = 0; j < bytes; j++) {
  81.     data[bytes * (x + y * dw) + j] = 
  82.       (source[bytes * (sx + sy * sw) + j] +
  83.        source[bytes * (sx2 + sy * sw) + j] +
  84.        source[bytes * (sx + sy2 * sw) + j] +
  85.        source[bytes * (sx2 + sy2 * sw) + j]) / 4;
  86.     // data[x + y * dw + j] = (j != 0) ? 255 : 0;
  87.       }
  88.     }
  89.   }
  90.   return data;
  91. }
  92.   
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.